/*
 * InputPanel.java
 */

package xq.swing;

import java.awt.Component;
import java.awt.LayoutManager;
import java.awt.Window;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.JPanel;

/**
 *
 * @author ThorntonRP
 */
public class InputPanel extends JPanel {
	
	private static final long serialVersionUID = 1L;

	public static enum MessageType {
		ERROR,
		INFORMATION,
		WARNING
	}
	
	public static class Message implements Serializable {
		
		private static final long serialVersionUID = 1L;
		
		private MessageType type;
		private String componentName;
		private String message;
		private Throwable thrown;

		public Message(MessageType type, String componentName, String message, Throwable thrown) {
			this.type = type;
			this.componentName = componentName;
			this.message = message;
			this.thrown = thrown;
		}

		public MessageType getType() {
			return type;
		}

		public String getComponentName() {
			return componentName;
		}
		
		public String getMessage() {
			return message;
		}

		public Throwable getThrown() {
			return thrown;
		}
	}

	private List<Message> messages = new ArrayList<Message>();
	
	public InputPanel() {
	}

	public InputPanel(boolean isDoubleBuffered) {
		super(isDoubleBuffered);
	}

	public InputPanel(LayoutManager layout) {
		super(layout);
	}

	public InputPanel(LayoutManager layout, boolean isDoubleBuffered) {
		super(layout, isDoubleBuffered);
	}

	public void add(Message message) {
		Object oldValue = Collections.unmodifiableList(new ArrayList<Message>(this.messages));
		messages.add(message);
		Object newValue = Collections.unmodifiableList(messages);
		firePropertyChange("messages", oldValue, newValue);
	}
	
	public void clearMessages() {
		List<Message> empty = Collections.emptyList();
		setMessages(empty);
	}
	
	public List<Message> getMessages() {
		return Collections.unmodifiableList(messages);
	}
	
	public void setMessages(List<Message> messages) {
		Object oldValue = Collections.unmodifiableList(this.messages);
		Object newValue = Collections.unmodifiableList(messages);
		this.messages = new ArrayList<Message>(messages);
		firePropertyChange("messages", oldValue, newValue);
	}
	
	public void packWindow() {
		Component c = getParent();
		boolean bContinue;
		do {
			bContinue = !(c == null || c instanceof Window);
			if (bContinue) {
				c = c.getParent();
			}
		} while (bContinue);
		if (c != null) {
			((Window) c).pack();
		}
	}
}